﻿using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
using Red.Core;

namespace Red.Launcher
{
    public static class Settings
    {
        #region Private Data

        private static string _perforceServerAddress = "uninitialised";
        private static ushort _perforceServerPort;
        private static string _databaseSource = "uninitialised";
        private static string _databaseName = "uninitialised";
        private static FilePath _editorPackagesInstallationDirectory = "uninitialised";
        private static FilePath _toolkitPackagesInstallationDirectory = "uninitialised";
        private static FilePath _enginePackagesInstallationDirectory = "uninitialised";
        private static FilePath _workspaceInstallationDirectory = "uninitialised";
        private static bool _displayRecentFilesByDefault;
        private static bool _addRuntimeToEnvironmentalVariables;
        private static bool _persistEnvironmentalVariablesAfterShutdown;

        #endregion Private Data

        #region Public Properties

        public static string ApplicationDataDirectoryPath
        {
            get
            {
                string userProfilePath = Environment.GetFolderPath( Environment.SpecialFolder.LocalApplicationData );
                string groupPath = "CD Projekt Red";
                string appPath = "Red Launcher";
                return Path.Combine( userProfilePath, groupPath, appPath );
            }
        }

        public static string PerforceServerAddress
        {
            get { return _perforceServerAddress; }
            set
            {
                _perforceServerAddress = value;
                OnPropertyChanged();
            }
        }

        public static ushort PerforceServerPort
        {
            get { return _perforceServerPort; }
            set
            {
                _perforceServerPort = value;
                OnPropertyChanged();
            }
        }

        public static string PerforceServer
        {
            get { return string.Format( @"{0}:{1}", PerforceServerAddress, PerforceServerPort ); }
        }

        public static string DatabaseServer
        {
            get { return _databaseSource; }
            set
            {
                _databaseSource = value;
                OnPropertyChanged();
            }
        }

        public static string DatabaseName
        {
            get { return _databaseName; }
            set
            {
                _databaseName = value;
                OnPropertyChanged();
            }
        }

        public static string DatabaseConnectionString
        {
            get { return @"Data Source=" + DatabaseServer + @";Integrated Security=True"; }
        }

        public static FilePath EditorPackagesInstallationDirectory
        {
            get { return _editorPackagesInstallationDirectory; }
            set
            {
                _editorPackagesInstallationDirectory = value;
                OnPropertyChanged();
            }
        }

        public static FilePath ToolkitPackagesInstallationDirectory
        {
            get { return _toolkitPackagesInstallationDirectory; }
            set
            {
                _toolkitPackagesInstallationDirectory = value;
                OnPropertyChanged();
            }
        }

        public static FilePath EnginePackagesInstallationDirectory
        {
            get { return _enginePackagesInstallationDirectory; }
            set
            {
                _enginePackagesInstallationDirectory = value;
                OnPropertyChanged();
            }
        }

        public static FilePath WorkspaceInstallationDirectory
        {
            get { return _workspaceInstallationDirectory; }
            set
            {
                _workspaceInstallationDirectory = value;
                OnPropertyChanged();
            }
        }

        public static bool DisplayRecentFilesByDefault
        {
            get { return _displayRecentFilesByDefault; }
            set
            {
                _displayRecentFilesByDefault = value;
                OnPropertyChanged();
            }
        }

        public static bool AddRuntimeToEnvironmentalVariables
        {
            get { return _addRuntimeToEnvironmentalVariables; }
            set
            {
                _addRuntimeToEnvironmentalVariables = value;
                OnPropertyChanged();
            }
        }

        public static bool PersistEnvironmentalVariablesAfterShutdown
        {
            get { return _persistEnvironmentalVariablesAfterShutdown; }
            set
            {
                _persistEnvironmentalVariablesAfterShutdown = value;
                OnPropertyChanged();
            }
        }

        #endregion Public Properties

        #region Property Changed Handler

        private static void OnPropertyChanged( [CallerMemberName] string propertyName = null )
        {
            StaticPropertyChanged?.Invoke( null, new PropertyChangedEventArgs( propertyName ) );
        }

        public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;

        #endregion Property Changed Handler
    }
}
